package com.mrkun.vueshop.controller;

import com.github.pagehelper.PageInfo;
import com.mrkun.vueshop.mapper.FundMapper;
import com.mrkun.vueshop.model.Fund;
import com.mrkun.vueshop.service.FundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author by yong
 * @date 2020/3/17
 */
@RestController
public class FundController {

    @Autowired
    private FundService fundService;

    @Autowired
    private FundMapper fundMapper;

    @GetMapping("/fundSearch/{page}/{size}")
    public PageInfo<Fund> getFunds(@PathVariable("page")Integer page,
                                   @PathVariable("size")Integer size,
                                   @RequestParam(value = "query")String query){
      return fundService.getFunds(page,size,"%"+query.trim()+"%");
    }

    @GetMapping("/getFundCount")
    public Integer getCount(){
        return fundMapper.count();
    }

    @GetMapping("/fund/findOne/{id}")
    public Fund getOne(@PathVariable("id")Integer id){
        return fundMapper.findOne(id);
    }

    @PostMapping("/fund/update")
    public String update(@RequestBody Fund fund){
        try{
            fundMapper.update(fund);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }

    @DeleteMapping("/fund/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        try{
            fundMapper.delete(id);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }

    @PostMapping("/fund/add")
    public String add(@RequestBody Fund fund){
        try{
            fundMapper.addFund(fund);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }
}
